<?php
class ControleURI
{
	public static $module;
    public static $control;
    public static $action;
    public static $file;
    public static $params = array();

    public static function getURI()
    {
    	$caminhos = array();

    	if($_SERVER['QUERY_STRING'] != "") {
    	   $caminhos = explode("/",$_SERVER['QUERY_STRING']);
    	}

    	$action = false;
    	$metodo_direto = false;

        foreach($caminhos as $key => $caminho)
        {
        	if($key == 0)
        	{
        		// Verifico se existe um controle
        		if (class_exists("Controle" . ucfirst($caminho))) {
        			self::$params['control'] = $caminho;
					self::$control = $caminho;
					self::$module = $caminho;
        		} else {
        			self::$params['control'] = "Index";
					self::$control = 'Index';
					self::$params['action'] = "index";
                    self::$action = "index";
                    self::$module = 'index';

                    if (method_exists("ControleIndex", $caminho))
                    {
                    	$key = 1;
                    }
        		}
        	}

        	if($key == 1)
        	{
                // Verifico se existe o metodo
        		if (method_exists("Controle" . ucfirst(self::$control), $caminho)){
                    self::$params['action'] = $caminho;
                    self::$action = $caminho;
                    $action = true;
                } else {
                	self::$params['action'] = "index";
                    self::$action = "index";
                }
        	}

        	if(!$action){
        		if($key % 2 != 0){
        			if(isset($caminhos[$key+1]))
        				self::$params[$caminhos[$key]] = $caminhos[$key+1];
        		}
        	} else if($key > 1 && !empty($caminhos[$key+1])){
        		if($key % 2 == 0){
        			if(isset($caminhos[$key+1]))
        				self::$params[$caminhos[$key]] = $caminhos[$key+1];
        		}
        	}
        }

        if (isset(self::$params["control"])) {
            self::$control = self::$params["control"];
        } else if (isset($_REQUEST["control"])) {
            self::$control = $_REQUEST["control"];
        } else {
            self::$control = "Index";
            self::$module = "index";
        }

        if (isset(self::$params["action"])) {
            self::$action = self::$params["action"];
        } else if (isset($_REQUEST["action"])) {
            self::$action = $_REQUEST["action"];
        } else {
            self::$action = "index";
        }

        // Verifico se foi passado algum arquivo
        if(isset($_FILES)) {
            self::$file = $_FILES;
        }

        // verificamos se existe os params, se sim faz um merge com os dados passados via post.
   		$parametros = array_pop($caminhos);
    	foreach(explode("&", $parametros) as $parametro) {
            $parametro = explode("=", $parametro);
            if(isset($parametro[1])) {
                self::$params[$parametro[0]] = $parametro[1];
            }
        }

        foreach($_REQUEST as $key => $value){
        	if(!empty($value)){
        		$param[$key] = $value;
        	}
        }

        if(isset($param) && count($param))
        	self::$params = array_merge(self::$params, $param);
    }

    public static function getUrlBase()
    {
        return preg_replace('#/[^/]+\.php5?$#', '', $_SERVER['SCRIPT_NAME']);
    }

    public static function getModule()
    {
    	return self::$module;
    }
}